package com.night.icm.controller.business;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.TermType;
import com.night.icm.model.business.StudentCourseGradeDTO;
import com.night.icm.service.business.service.StudentCourseGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * StudentGradeController
 * 教师批改生成学生成绩
 * 学生查看成绩
 *
 * @author Liao ZhiYong
 * @date 2020/2/12
 */
@Api(tags = "学生成绩管理")
@RestController
@RequestMapping("/api/icm-student-grade")
public class StudentCourseGradeController {

    private final StudentCourseGradeService studentCourseGradeService;

    @Autowired
    public StudentCourseGradeController(StudentCourseGradeService studentCourseGradeService) {
        this.studentCourseGradeService = studentCourseGradeService;
    }

    @ApiOperation(value = "分页查找学生成绩")
    @GetMapping(value = "")
    public PageDataDTO<StudentCourseGradeDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "学院名称") @RequestParam(value = "academyName", required = false) String academyName,
            @ApiParam(value = "班级名称") @RequestParam(value = "className", required = false) String className,
            @ApiParam(value = "学生姓名") @RequestParam(value = "studentName", required = false) String studentName,
            @ApiParam(value = "课程名称") @RequestParam(value = "courseName", required = false) String courseName,
            @ApiParam(value = "授课老师id") @RequestParam(value = "teacherId", required = false) String teacherId,
            @ApiParam(value = "所属年度") @RequestParam(value = "year", required = false) String year,
            @ApiParam(value = "所属学期") @RequestParam(value = "termType", required = false) TermType termType,
            @ApiParam(value = "是否批改") @RequestParam(value = "isCorrect", required = false) Boolean isCorrect
    ) {
        return this.studentCourseGradeService.findOnePage(page, size, sort, academyName, className, studentName, courseName, teacherId, year, termType, isCorrect);
    }

    @ApiOperation(value = "提交批改的学生成绩")
    @PutMapping(value = "/{id}")
    public StudentCourseGradeDTO complete(
            @ApiParam(value = "学生课程成绩id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "学生课程成绩", required = true) @RequestBody @Validated StudentCourseGradeDTO studentCourseGradeDTO
    ) {
        return this.studentCourseGradeService.complete(id, studentCourseGradeDTO);
    }

    @ApiOperation(value = "修改已批改的学生成绩")
    @PutMapping(value = "/update/{id}")
    public StudentCourseGradeDTO update(
            @ApiParam(value = "学生课程成绩id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "学生课程成绩", required = true) @RequestBody @Validated StudentCourseGradeDTO studentCourseGradeDTO
    ) {
        return this.studentCourseGradeService.update(id, studentCourseGradeDTO);
    }

    @ApiOperation(value = "模糊查询学生课程成绩")
    @GetMapping(value = "/search")
    public List<StudentCourseGradeDTO> search(
            @ApiParam(value = "学生code") @RequestParam(value = "studentCode", required = false) String studentCode,
            @ApiParam(value = "所属年度") @RequestParam(value = "year", required = false) String year,
            @ApiParam(value = "所属学期") @RequestParam(value = "termType", required = false) TermType termType
    ) {
        return this.studentCourseGradeService.search(studentCode, year, termType);
    }

    @ApiOperation(value = "通过id查找学生课程成绩")
    @GetMapping(value = "/{id}")
    public StudentCourseGradeDTO findById(@ApiParam(value = "学生课程成绩id", required = true) @PathVariable(name = "id") String id) {
        return this.studentCourseGradeService.findById(id);
    }

    @ApiOperation(value = "根据学生id查找学生所有课程成绩")
    @GetMapping(value = "/findAll/{studentId}")
    public List<StudentCourseGradeDTO> findAllByStudentId(
            @ApiParam(value = "学生id", required = true) @PathVariable(name = "studentId") String studentId
    ) {
        return this.studentCourseGradeService.findAllByStudentId(studentId);
    }
}